

import NProgress from 'nprogress' // 导入 nprogress模块
import 'nprogress/nprogress.css' // 导入样式，否则看不到效果
NProgress.configure({ showSpinner: false }) // 显示右上角螺旋加载提示
import store from '@/store/index'
let baseURL = localStorage.getItem('BASE_URL')
import axios from 'axios'
import { ElMessage, ElMessageBox } from "element-plus";

function request(options) {
    // 1,创建一个axios实例
    return new Promise((resolve, reject) => {
        const instance = axios.create({
            baseURL: baseURL,
            timeout: 3000,
            method: options.method ? options.method : 'get'
            // headers: {
            //     "Content-Type": options.requestType ?
            //         "multipart/form-data" : "application/json",
            // },
        })
        // 请求拦截配置
        instance.interceptors.request.use(
            (config) => {
                // loadingInstance = Loading.service()
                NProgress.start() //开启进度条


                if (store.state.UserToken) {
                    config.headers.Authorization = store.state.UserToken
                }
                return config
            },
            (error) => {
                return Promise.reject(error)
            }
        )

        // 响应拦截配置 interceptors
        instance.interceptors.response.use(
            (response) => {
                // loadingInstance.close()
                NProgress.done() //完成进度条

                return response.data
            },
            (err) => {
                if (err && err.respones) {
                    // console.log(err.respones,'err')
                    switch (err.respones.status) {
                        case 400:
                            err.message = '请求出错'
                            break

                        case 401:

                            ElMessage({
                                showClose: true,
                                message: "授权失败，请重新登录!",
                                type: "warning",
                            });
                            store.commit('LOGIN_OUT')
                            setTimeout(() => {
                                window.location.reload()
                            }, 1000);
                            break
                        case 403:
                            err.message = '拒绝访问'
                            break
                        case 404:
                            err.message = '请求错误，未找到服务器'
                            break
                        case 500:
                            err.message = '服务器出错'
                            break
                    }
                } else {
                    err.message = '服务器连接失败'
                }

                ElMessage({
                    showClose: true,
                    message: err.message,
                    type: "error",
                });
                return Promise.reject(err.respones)
            }
        )

        instance(options)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}

export default request
